package com.xinqi.modules.chat.chat.message.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.chat.chat.message.domain.ChatTopMessageEntity;
import com.xinqi.modules.chat.chat.message.manager.ChatTopMessageManager;
import com.xinqi.modules.chat.chat.message.mapper.ChatTopMessageMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 聊天消息置顶表管理层实现
 *
 * @author: yyzhen
 * @date: 2023/06/29
 */
@Service
@RequiredArgsConstructor
public class ChatTopMessageManagerImpl extends BaseServiceImpl<ChatTopMessageMapper, ChatTopMessageEntity> implements ChatTopMessageManager {

    /**
     * 查询会话下置顶消息
     *
     * @param targetId
     * @return
     */
    @Override
    public List<ChatTopMessageEntity> listByTargetId(String targetId) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(targetId), MessageUtils.message("chat.message.top.targetId.not.blank"));
        LambdaQueryWrapper<ChatTopMessageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatTopMessageEntity::getTargetId, targetId)
            .orderByDesc(ChatTopMessageEntity::getTopTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据会话ID和消息ID查询
     *
     * @param targetId
     * @param messageId
     * @return
     */
    @Override
    public ChatTopMessageEntity infoByTargetIdAndMessageId(String targetId, String messageId) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(targetId), MessageUtils.message("chat.message.top.targetId.not.blank"));
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(messageId), MessageUtils.message("chat.message.top.messageId.not.blank"));
        LambdaQueryWrapper<ChatTopMessageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatTopMessageEntity::getTargetId, targetId)
            .eq(ChatTopMessageEntity::getMessageId, messageId)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据会话ID和消息ID删除
     *
     * @param targetId
     * @param messageId
     * @return
     */
    @Override
    public boolean removeByTargetIdAndMessageId(String targetId, String messageId) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(targetId), MessageUtils.message("chat.message.top.targetId.not.blank"));
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(messageId), MessageUtils.message("chat.message.top.messageId.not.blank"));
        LambdaQueryWrapper<ChatTopMessageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatTopMessageEntity::getTargetId, targetId)
            .eq(ChatTopMessageEntity::getMessageId, messageId);
        return this.baseMapper.delete(queryWrapper) > 0;
    }

    /**
     * 查询会话下最早的一条置顶消息
     *
     * @param targetId
     * @return
     */
    @Override
    public ChatTopMessageEntity selectOneByTargetIdOrderByTopTime(String targetId) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(targetId), MessageUtils.message("chat.message.top.targetId.not.blank"));
        LambdaQueryWrapper<ChatTopMessageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatTopMessageEntity::getTargetId, targetId)
            .orderByAsc(ChatTopMessageEntity::getTopTime)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询会话下置顶消息数量
     *
     * @param targetId
     * @return
     */
    @Override
    public Long countByTargetId(String targetId) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(targetId), MessageUtils.message("chat.message.top.targetId.not.blank"));
        LambdaQueryWrapper<ChatTopMessageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatTopMessageEntity::getTargetId, targetId);
        return this.baseMapper.selectCount(queryWrapper);
    }

}
